/*
 * @Author: huangtianyang 916072572@qq.com
 * @Date: 2023-11-15 15:08:50
 * @LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @LastEditTime: 2024-06-03 10:47:20
 * @FilePath: \driving-training-admin\src\views\classManagement\learnDrivingTypeList\edit\index.ts
 */
import { reactive, ref, watchEffect } from 'vue'
import { useRoute } from 'vue-router'
import { data } from './formConfig'
import { fileListProps, fileProps, formDataInterface, PropsFormpData } from '@/utils/interface'
import router from '@/router'
import { routerStore } from '@/store/router.store'
import { ElMessage } from 'element-plus'
import { userStore } from '@/store/user.store'
import {
    addLearnDrivingType,
    classTypeListBySchoolId,
    editLearnDrivingTypes,
    learnDrivingTypeDetailed,
    learnDrivingTypeOperationInterface,
    productPicProps
} from '@/apis/learnDrivingType'
import { describeInterface } from '@/apis/drivingSchool'
import { addDisplayBlockToImages } from '@/utils'
import { DICT_DATA } from '@/utils/dict-data'

interface tagInterface {
    label: string
    describe: string
    id: ''
}

export default class LearnDrivingTypeOpreation {
    formData = reactive<PropsFormpData[]>([...data])
    //编辑类型 create | edit | look
    operation = reactive({
        type: 'create',
        id: ''
    })
    routerStore = routerStore()
    uStore = userStore()
    constructor() {
        const route = useRoute()
        this.operation.type = route.query.type as string
        this.operation.id = route.query?.id as string
        console.log('route', route)

        const options: { label: string; value: string; cityId: string; cityName: string }[] = []
        this.uStore.queryDrivingSchoolSimple((res: Map<string, any>) => {
            res.forEach(item => {
                options.push({ label: item.shortName, value: item.id, cityId: item.cityId, cityName: item.city })
            })

            this.formData.forEach((item: PropsFormpData, index: number) => {
                if (Object.prototype.toString.call(item) === '[object Array]' && index === 0) {
                    const childList = item as formDataInterface[]
                    childList[0].options = options
                } else if (Object.prototype.toString.call(item) === '[object Array]' && index === 3) {
                    //产品类型
                    const childList = item as formDataInterface[]
                    const a: any[] = []
                    DICT_DATA.get('productType').forEach((p: any) => {
                        a.push({
                            label: p.label,
                            value: p.value
                        })
                    })
                    childList[0].options = a
                }
            })
        })

        if (this.operation.type !== 'create') {
            this.getData()
        }

        watchEffect(() => {
            this.formData.forEach((item: PropsFormpData, index: number) => {
                if (Object.prototype.toString.call(item) === '[object Array]' && index === 0) {
                    const childList = item as formDataInterface[]
                    if (childList[0].value) {
                        //选择驾校后,默认选择城市
                        options.forEach(ds => {
                            if (ds.value === childList[0].value) {
                                this.formData.forEach((d: PropsFormpData, index: number) => {
                                    if (Object.prototype.toString.call(d) === '[object Array]' && index === 1) {
                                        const e = d as formDataInterface[]
                                        // e[0].value = ds.cityId.split('/')
                                        const cityId: any[] = ds.cityId?.split('/')
                                        const cityName: any[] = ds.cityName?.split('/')
                                        e[0].value = [
                                            { label: cityName[0], id: cityId[0] },
                                            { label: cityName[1], id: cityId[1] }
                                        ]
                                    }
                                })
                            }
                        })

                        classTypeListBySchoolId(childList[0].value as string).then(res => {
                            if (Number(res.data.code) === 200) {
                                this.formData.forEach((d: PropsFormpData, index: number) => {
                                    if (Object.prototype.toString.call(d) === '[object Array]' && index === 2) {
                                        const e = d as formDataInterface[]
                                        e[0].options = res.data.result.map((item: any) => {
                                            return {
                                                label: item.className,
                                                value: item.id
                                            }
                                        })
                                    }
                                })
                            }
                        })
                    }
                }
            })
        })
    }
    getData = (arg?: any) => {
        learnDrivingTypeDetailed(this.operation.id).then(res => {
            if (Number(res.data.code) === 200) {
                const result = res.data.result
                this.formData.forEach((child: PropsFormpData) => {
                    if (Object.prototype.toString.call(child) === '[object Array]') {
                        const childList = child as formDataInterface[]
                        childList.forEach((item: formDataInterface) => {
                            if (item.key === 'activePrice') {
                                item.value = result.activePrice as string
                            } else if (item.key === 'city') {
                                const cityId: any[] = result.cityId?.split('/')
                                const cityName: any[] = result.city?.split('/')
                                item.value = [
                                    { label: cityName[0], id: cityId[0] },
                                    { label: cityName[1], id: cityId[1] }
                                ]
                                console.log('cityId', cityId, cityName, item.value)
                            } else if (item.key === 'classId') {
                                item.value = result.classId as string
                            } else if (item.key === 'schoolId') {
                                item.value = result.schoolId as string
                            } else if (item.key === 'description') {
                                item.value = result.description as string
                            } else if (item.key === 'details') {
                                item.value = result.details as string
                            } else if (item.key === 'originalPrice') {
                                item.value = result.originalPrice as string
                            } else if (item.key === 'productName') {
                                item.value = result.productName as string
                            } else if (item.key === 'productType') {
                                item.value = result.productType + ''
                            } else if (item.key === 'purchaseNum') {
                                item.value = result.purchaseNum as string
                            } else if (item.key === 'productPic') {
                                const imageArray: fileProps[] = []

                                result.productPic.forEach((f: any, index: number) => {
                                    imageArray.push({
                                        index,
                                        tag: f.tag,
                                        type: 'image',
                                        list: [
                                            {
                                                key: f.pic,
                                                name: f.tag
                                            }
                                        ]
                                    })
                                })
                                item.value = imageArray
                            }
                        })
                    }
                })
            }
        })
    }
    onCancel = () => {
        this.routerStore.removeHeaderRoute(this.routerStore.selectIndex)
        router.push('/learnDrivingTypeList')
    }

    //保存
    onSave = (formRef: any) => {
        if (formRef?.validateForm()) {
            const data: learnDrivingTypeOperationInterface = {
                activePrice: 0, //活动价
                city: '', //城市
                classId: '', //班别id
                description: '', //产品描述
                details: '', //介绍说明
                originalPrice: 0, //原价
                productName: '', //产品名称
                productPic: [], //产品图片
                productType: 0, //1:c1, 2:c2, 3:抵扣卷
                purchaseNum: '', //购买基数
                schoolId: '',
                cityId: ''
            }

            this.formData.forEach((child: PropsFormpData) => {
                if (Object.prototype.toString.call(child) === '[object Array]') {
                    const childList = child as formDataInterface[]
                    childList.forEach((item: formDataInterface) => {
                        console.log(item)
                        if (item.key === 'activePrice') {
                            data.activePrice = Number(item.value)
                        } else if (item.key === 'city') {
                            //所在城市
                            const arr = item.value as { label: string; id: string }[]
                            data.city = arr[0].label + '/' + arr[1].label
                            data.cityId = arr[0].id + '/' + arr[1].id

                            console.log('city', arr, data.city)
                            // data.city = (item.value as string[]).join('/')
                        } else if (item.key === 'classId') {
                            data.classId = item.value as string
                        } else if (item.key === 'schoolId') {
                            data.schoolId = item.value as string
                        } else if (item.key === 'description') {
                            data.description = item.value as string
                        } else if (item.key === 'details') {
                            data.details = addDisplayBlockToImages(item.value as string)
                        } else if (item.key === 'originalPrice') {
                            data.originalPrice = Number(item.value)
                        } else if (item.key === 'productName') {
                            data.productName = item.value as string
                        } else if (item.key === 'productType') {
                            data.productType = Number(item.value)
                        } else if (item.key === 'purchaseNum') {
                            data.purchaseNum = item.value as string
                        } else if (item.key === 'productPic') {
                            const arr: fileProps[] = item.value as fileProps[]
                            const productPic: productPicProps[] = []
                            arr.forEach(item => {
                                item.list.forEach((list: fileListProps) => {
                                    productPic.push({ pic: list.key, tag: list.name })
                                })
                            })
                            data.productPic = productPic
                        }
                    })
                }
            })
            console.log('data', data)
            if (this.operation.type === 'create') {
                addLearnDrivingType(data).then(res => {
                    if (Number(res.data.code) === 200) {
                        ElMessage.success('新增成功！')
                        this.routerStore.removeHeaderRoute(this.routerStore.selectIndex)
                        router.push('/learnDrivingTypeList')
                    }
                })
            } else {
                data.id = this.operation.id
                editLearnDrivingTypes(data).then(res => {
                    if (Number(res.data.code) === 200) {
                        ElMessage.success('修改成功！')
                        this.routerStore.removeHeaderRoute(this.routerStore.selectIndex)
                        router.push('/learnDrivingTypeList')
                    }
                })
            }
        }
    }
}
